package camid.util.dataanalysis;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map.Entry;

public class DataAnalysisScript {

	public static File getLastExperimentDir(String exp)
	{
		File parentDir = new File("/home/diego/development/workspace-middleware/camid3/results/aws-" + exp);
		long srctime = 0;
		File expDir = null;

		for(File file : parentDir.listFiles())
		{
			long time = file.lastModified();
			if(time >= srctime)
			{
				srctime = time;
				expDir = file;
			}
		}
		return expDir;
	}

	public static void main(String[] args) throws IOException 
	{
		String[] experiments = new String[]{
//				"elasticmigration","elasticnomigration",
//				"elasticreplication","elasticnoreplication",
				"directreplication","directmigration"
				//				"migration","nomigration",
				//				"replication","noreplication"
		};
		File statsDir = new File("statistics");
		statsDir.mkdirs();

		FileInputStream fis = null;


		FileOutputStream responseTimeFos = new FileOutputStream(new File(statsDir,"responsetime.csv"));
		FileOutputStream downtimeFos = new FileOutputStream(new File(statsDir,"downtime.csv"));

		for(String exp : experiments)
		{	
			File dir = getLastExperimentDir(exp);
			dir.length();

			DowntimeLogAnalyser analyser = new DowntimeLogAnalyser();

			fis = new FileInputStream(new File(dir,"jmeter-sdout.log"));
			analyser.analyseStream(fis);
			fis.close();

			analyser.toStream(downtimeFos, exp);
			//			
			//			fos = new FileOutputStream(new File(statsDir,"downtime-" + exp + "-avgs.csv"));
			//			analyser.avgsToStream(fos);
			//			fos.close();


			fis = new FileInputStream(new File(dir,"expMiddleware.log"));
			//			
			JMeterAnalyser jmanalyser = new JMeterAnalyser();
			jmanalyser.analyseStream(fis);
			//			
			fis.close();

//			try {
//				fos = new FileOutputStream(new File(statsDir,String.format("timestamp-%s.csv", exp)));	
//
//				for (Entry<JMeterTG, JMeterMetricSet> entry : jmanalyser.getMetricSets().entrySet())
//				{
//					JMeterMetricSet metricSet = entry.getValue();
//					JMeterTimestampAnalyser tstampAnalyser = new JMeterTimestampAnalyser(metricSet);
//					tstampAnalyser.analyse();
//					tstampAnalyser.toStream(fos);
//				}
//			}
//			finally
//			{
//				fos.close();
//			}


			jmanalyser.toStream(responseTimeFos, exp);

		}

		downtimeFos.close();

		responseTimeFos.close();

	}

}
